using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Data.SqlClient;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	/// <summary>
	/// Summary description for DataObjectDBQueryLoader.
	/// </summary>
	class DataObjectDBQueryWorker
	{
  
		public static void queryDoc( Form form, DataADOAdapter adoAdapter )
		{
			string where = buildQueryWhere( form );
			string sql = DBUtil.buildQuerySQL( adoAdapter.CommandText, where );
			
			DataSet dataSet = new DataSet();
			try
			{
				DBContext dbContext = new DBContext( adoAdapter, sql );
				dbContext.DbDataAdapter.Fill( dataSet, adoAdapter.TableName );
				dbContext.close();
			}
			catch( Exception e )
			{
				throw e;
			}
			XmlDocument dataDoc = DBDataSetXMLWriter.writeDataSet( dataSet, adoAdapter.TableName );
			
			if( form.Manifest.IsADOSubmit )
			{
				FormSession.updateDataSet( form, dataSet);
			}

            //CID 0906 //2011.05.12 //Begin
			XmlElement dataFieldsElement = setDataFields( form, dataDoc );
            //CID 0907 //2011.05.13 //Begin
            FormCalculatedFieldWorker.calcaulteGroupNode(form, dataFieldsElement, null, false);
            //CID 0907 //2011.05.13 //End
            //CID 0906 //2011.05.12 //End
		}

		private static String buildQueryWhere( Form form )
		{
			string[] queryFieldsName = new string[]{ "queryFields" };
			XmlElement queryFieldsElement = DOMUtil.findElementByLocalNames( form.XmlDoc, queryFieldsName );
			XmlElement queryElement = DOMUtil.findFirstChildElement( queryFieldsElement );
			if( queryElement == null )
			{
				return "";
			}
			StringBuilder sql = new StringBuilder();
			foreach( XmlAttribute attr in queryElement.Attributes )
			{
				if( (attr.Name.IndexOf( ":" ) < 0) && 
					(!attr.Name.Equals( "xmlns" )) && 
					(!attr.Name.Equals( JetConstant.JetId )) )
				{
					if( attr.Value.Length > 0 )
					{
						if( sql.Length > 0 )
						{
							sql.Append( " and " );
						}
						sql.Append( attr.Name + " = '" + attr.Value + "' " );
					}
				}
			}
			if( sql.Length > 0 )
			{
				sql.Insert( 0, " where " );
				return sql.ToString();
			}
			else
			{
				return "";
			}
		}

		private static XmlElement setDataFields( Form form, XmlDocument dataDoc )
		{
			form.XmlDoc.DocumentElement.SetAttribute( "xmlns:xdado", JetConstant.URI_XDADO ); 
			string[] dataFieldsName = new string[]{ "dataFields" };
			XmlElement dataFieldsElement = DOMUtil.findElementByLocalNames( form.XmlDoc, dataFieldsName );
			DOMUtil.removeChildNodes( dataFieldsElement );

			XmlNodeList recordElementList = dataDoc.DocumentElement.SelectNodes( "./*" );
			foreach( XmlElement recordElement in recordElementList )
			{
				XmlElement cloneElement = CloneUtil.cloneNsElement( form.XmlDoc, recordElement, "d", JetConstant.URI_D );
				dataFieldsElement.AppendChild( cloneElement );
			}

			recordElementList = dataFieldsElement.SelectNodes( "./*" );
			setRecOrder( recordElementList );

            //CID 0906 //2011.05.12 //Begin
			return dataFieldsElement;
            //CID 0906 //2011.05.12 //End
		}

		private static void setRecOrder( XmlNodeList recordElementList )
		{
			int index = 0;
			foreach( XmlElement recordElement in recordElementList )
			{
				XmlAttribute recOrderAttr = recordElement.OwnerDocument.CreateAttribute( "xdado", JetConstant.InfoJetADORecOrderLocalName, JetConstant.URI_XDADO );
				recOrderAttr.Value = index + "";
				recordElement.SetAttributeNode( recOrderAttr );
				XmlNodeList subRecordElementList = recordElement.SelectNodes( "./*" );
				setRecOrder( subRecordElementList );
				index ++;
			}
		}

	}
}
